import time

from django.db import models
from django.utils import timezone

from BYD_ERP.utils.base_model import BaseModel, BaseModel2

# python ../../manage.py startapp production_info

# 迁移基础：goods_info, erp_system

# python ../../manage.py makemigrations production_info
# python ../../manage.py migrate production_info

# python ../../manage.py check


# BOM指Bill of Material， 即物料清单，
#     主要用来记录一个产品所用到的下阶材料及相关属性，亦即母件与所有子件的从属关系、单位用量及其他属性。


'''
    既可以用2*5，也可以用2*3，
    BOM物料表，可以加上一项：选择“方案一”“方案二”“方案三”…………
'''


# BOM表中的物料模型类
###################################
# 后期看下，BOM物料模型是单条合适，还是单筒合适。
###################################
class BomMaterialModel(BaseModel2):
    # 冗余字段
    goods_code = models.CharField('货品编号，不让用户填写', max_length=28)
    goods_name = models.CharField('货品名称', max_length=20)
    specification = models.CharField('规格/件号', max_length=50, blank=True, null=True)
    model_number = models.CharField('国标型号', max_length=50, blank=True, null=True)
    partner = models.ForeignKey('goods_info.PartnerModel', related_name='partner_bom_list', on_delete=models.SET_NULL,
                                blank=True, null=True, verbose_name='合作厂家')
    brand = models.CharField('品牌', max_length=50, blank=True, null=True)
    section = models.ForeignKey('goods_info.SectionModel', related_name='section_bom_list', on_delete=models.SET_NULL,
                                blank=True, null=True, verbose_name='截面/断面')  # 6PK/8PK/10PK/AV13/AV15/AV17
    units = models.ForeignKey("goods_info.UnitsModel", on_delete=models.SET_NULL, blank=True, null=True,
                              verbose_name='计量单位')
    units_name = models.CharField('计量单位名字', max_length=50, blank=True, null=True)
    expiration_day = models.IntegerField('保质期', blank=True, null=True)
    cost_price = models.DecimalField('材料成本单价（采购单价）', max_digits=10, decimal_places=2, blank=True, null=True)
    bom_count = models.DecimalField('数量，最多精确到小数点后四位', max_digits=10, decimal_places=4, blank=True, null=True)
    material_money = models.DecimalField('材料费，最多精确到小数点后四位', max_digits=10, decimal_places=4, blank=True, null=True)

    goods = models.ForeignKey('goods_info.GoodsModel', related_name='item_list_goods_bom', on_delete=models.SET_NULL,
                              verbose_name='货品', blank=True, null=True)
    parent_goods = models.ForeignKey('goods_info.GoodsModel', related_name='bom_material_list', blank=True, null=True,
                                     on_delete=models.SET_NULL, verbose_name='当前的物料清单属于哪个货品')

    images_list = models.CharField('货品图片（实物拍照、标签等）所对应的ID列表', max_length=20, blank=True, null=True)  # 字段的值为：'1,2,3,4'
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    class Meta:
        db_table = 't_bom_material'
        verbose_name = 'BOM生产物料表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.goods_name


'''
    既可以用2*5，也可以用2*3，
    BOM工序表，可以加上一项：选择“方案一”“方案二”“方案三”…………
'''


# BOM表中生产工序模型类
class BomProcessModel(BaseModel2):
    process_order = models.IntegerField('执行顺序')
    name = models.CharField('工序名称', max_length=50)
    leader_user = models.ForeignKey('erp_system.UserModel', related_name='leader_bom_process_list', blank=True,
                                    null=True, on_delete=models.SET_NULL, verbose_name='负责人员')
    leader_name = models.CharField('负责人的名字', max_length=20, blank=True, null=True)
    money = models.DecimalField('工价，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)
    parent_goods = models.ForeignKey('goods_info.GoodsModel', related_name='bom_process_list', blank=True, null=True,
                                     on_delete=models.SET_NULL, verbose_name='当前的物料清单属于哪个货品')
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    class Meta:
        db_table = 't_bom_process'
        verbose_name = 'BOM生产工序表'
        verbose_name_plural = verbose_name
        ordering = ['process_order']

    def __str__(self):
        return self.name


# 每日生产任务单模型类
class DailyProductTaskModel(BaseModel2):
    invoice_date = models.DateTimeField('制单日期', blank=True, null=True, default=timezone.now)
    task_date = models.DateTimeField('生产日期', blank=True, null=True)
    number_code = models.CharField('单据编号/单据编号条码', max_length=28, unique=True)

    # 制单人（每日生产任务单）
    operator_user = models.ForeignKey('erp_system.UserModel', related_name='daily_product_task_list', blank=True,
                                      null=True, on_delete=models.SET_NULL, verbose_name='制单人（每日生产任务单），不能修改')

    # 增加一个冗余字段
    operator_user_name = models.CharField('制单人真实姓名', max_length=20)

    attachment_list = models.CharField('附件的ID列表，字段的值为：1,2,3,4', max_length=20, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    class Meta:
        db_table = 't_daily_product_task'
        verbose_name = '每日生产任务单'
        verbose_name_plural = verbose_name
        ordering = ['task_date']


# 每日生产任务单项目模型类
class DailyProductTaskItemModel(BaseModel2):
    # 冗余字段
    name = models.CharField('货品名称', max_length=20)
    partner = models.ForeignKey('goods_info.PartnerModel', on_delete=models.SET_NULL, blank=True,
                                null=True, verbose_name='合作厂家')
    partner_name = models.CharField('合作方/厂家的名称', max_length=50, blank=True, null=True)
    specification = models.CharField('件号/规格', max_length=50, blank=True, null=True)
    model_number = models.CharField('国标型号', max_length=50, blank=True, null=True)
    section_name = models.CharField('产品断面/截面', max_length=20, blank=True, null=True)  # eg.AV13/AV17/6PK/8PK
    length = models.CharField('型号长度mm', max_length=50, blank=True, null=True)

    mould = models.ForeignKey('basic_info.MouldModel', on_delete=models.SET_NULL, verbose_name='模具', blank=True,
                              null=True)
    mould_name = models.CharField('模具型号', max_length=20, blank=True, null=True)

    # glue：胶料
    glue_name = models.CharField('胶料', max_length=50, blank=True, null=True)  # X13/X8/X10/X11
    # rope：线绳
    rope = models.CharField('线绳', max_length=50, blank=True, null=True)  # 2*3/2*5

    primary_units_name = models.CharField('货品的主计量单位（条）', max_length=20, blank=True, null=True)
    auxiliary_units_name = models.CharField('货品的辅计量单位（模）', max_length=20, blank=True, null=True)
    primary_units = models.ForeignKey("goods_info.UnitsModel", related_name='primary_units_list',
                                      on_delete=models.SET_NULL, blank=True, null=True, verbose_name='主计量单位')
    auxiliary_units = models.ForeignKey("goods_info.UnitsModel", related_name='auxiliary_units_list',
                                        on_delete=models.SET_NULL, blank=True, null=True, verbose_name='辅计量单位')
    plan_completion_date = models.DateField('计划完工日期', blank=True, null=True)

    # 定额条数的来源：维护了生产工艺（就是宽度，国标或者盖茨）后，才能确定下定额条数来。
    quota_quantity = models.DecimalField('定额条数/定额数量', max_digits=10, decimal_places=0, blank=True, null=True)

    # 本月经营部的生产计划（条）：本月经营部多次提出的生产计划的累加和
    operation_department_production_plan_tiao = models.DecimalField('本月经营部的生产计划（条）', max_digits=10, decimal_places=0,
                                                                    default=1, blank=True, null=True)
    # 转换成模的时候，向上取整，除以定额条数，再除以标准合格率
    operation_department_production_plan_mu = models.DecimalField('本月经营部的生产计划（模）', max_digits=10, decimal_places=0,
                                                                  default=1, blank=True, null=True)
    # forming process：成型工序
    # vulcanization：橡胶的硫化

    already_arranged_unvulcanized_mu = models.DecimalField('本月车间已安排未硫化的带筒数（模/筒）', max_digits=10, decimal_places=0,
                                                           default=1, blank=True, null=True)

    vulcanized_mu = models.DecimalField('本月车间已硫化的带筒数（模/筒）', max_digits=10, decimal_places=0, default=1,
                                        blank=True, null=True)

    vulcanized_tiao = models.DecimalField('本月车间已硫化的定额条数（条）', max_digits=10, decimal_places=0, default=1,
                                          blank=True, null=True)

    to_be_arranged_mu = models.DecimalField('本月车间待安排的生产计划（模/筒）', max_digits=10, decimal_places=0, default=1,
                                            blank=True, null=True)

    daily_production_plan_mu = models.DecimalField('车间每日生产计划（模/筒）', max_digits=10, decimal_places=0, default=1,
                                                   blank=True, null=True)

    daily_product_task = models.ForeignKey('DailyProductTaskModel', related_name='daily_product_task_item_list',
                                           blank=True, null=True, on_delete=models.CASCADE, verbose_name='每日生产任务单')

    goods = models.ForeignKey('goods_info.GoodsModel', on_delete=models.SET_NULL, verbose_name='货品', blank=True,
                              null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    class Meta:
        db_table = 't_daily_product_task_item'
        verbose_name = '每日生产任务单项目'
        verbose_name_plural = verbose_name
        ordering = ['id']


# 每日生产物料模型
#    （记录每天安排的产品需要的各种材料的总量）
class DailyProductMaterialModel(BaseModel2):
    goods_code = models.CharField('货品编号，不让用户填写', max_length=28)
    name = models.CharField('货品名称', max_length=20)
    specification = models.CharField('件号/规格', max_length=50, blank=True, null=True)
    model_number = models.CharField('国标型号', max_length=50, blank=True, null=True)
    units_name = models.CharField('计量单位', max_length=20, blank=True, null=True)
    units = models.ForeignKey("goods_info.UnitsModel", on_delete=models.SET_NULL, blank=True, null=True,
                              verbose_name='计量单位')
    daily_count = models.DecimalField('每天生产任务需要的数量，最多精确到小数点后四位', max_digits=10, decimal_places=4, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    goods = models.ForeignKey('goods_info.GoodsModel', on_delete=models.SET_NULL, verbose_name='货品', blank=True,
                              null=True)

    get_count = models.DecimalField('已经领取的数量，最多精确到小数点后四位', max_digits=10, decimal_places=4, default=0)
    back_count = models.DecimalField('退回的数量，最多精确到小数点后四位', max_digits=10, decimal_places=4, default=0)
    use_count = models.DecimalField('已使用的数量，最多精确到小数点后四位', max_digits=10, decimal_places=4, default=0)
    scrap_count = models.DecimalField('报废的数量，最多精确到小数点后四位', max_digits=10, decimal_places=4, default=0)

    daily_product_task = models.ForeignKey('DailyProductTaskModel', related_name='daily_material_list', blank=True,
                                           null=True, on_delete=models.CASCADE, verbose_name='每日生产任务单')


    class Meta:
        db_table = 't_daily_product_material'
        verbose_name = '每日生产任务物料表'
        verbose_name_plural = verbose_name
        ordering = ['id']


# 每日生产任务中的生产工序模型类
class DailyProductProcessModel(BaseModel2):
    process_order = models.IntegerField('执行顺序')
    name = models.CharField('工序名称', max_length=50)
    leader_user = models.ForeignKey('erp_system.UserModel', related_name='leader_daily_process_list', blank=True,
                                    null=True, on_delete=models.SET_NULL, verbose_name='操作工')
    leader_name = models.CharField('操作工的真实名字', max_length=20, blank=True, null=True)

    daily_product_task = models.ForeignKey('DailyProductTaskModel', related_name='daily_process_list',
                                           blank=True, null=True, on_delete=models.CASCADE, verbose_name='每日生产任务单')

    remark = models.CharField('备注', max_length=512, blank=True, null=True)

    class Meta:
        db_table = 't_daily_product_process'
        verbose_name = '每日生产任务工序表'
        verbose_name_plural = verbose_name
        ordering = ['process_order']
